﻿//===============================================================================
// Apolineo Patterns & Practices LeanEngine Framework
//===============================================================================
// Copyright © Apolineo Desenvolvimento de Sistemas Ltda.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LeanEngine.Entity;
using LeanEngine.Extensions;
using LeanEngine.SalesConcept.Entity;

namespace LeanEngine.SalesConcept.Windows.Control
{
    public partial class MaintenanceControlOfSale : UserControl, IMaintenanceControl<Sale>
    {
        #region Constructor Members

        public MaintenanceControlOfSale()
        {
            InitializeComponent();

            this.CurrentSale = new Sale();
            this.selCustomer.FormSelect = new FormSelectCustomer();
            this.selSalesman.FormSelect = new FormSelectSalesman();
        }

        #endregion

        #region Property Members

        private Sale CurrentSale { get; set; }

        //Através do controle de integração dos validadores com controles de tela
        //PropertyValidatorProvider (LeanEngine.Validation.Windows.Forms) o lob se encarregra de
        //executar os validadores passando os valores do controle e retornando
        //na propriedade ErrorMessages as mensagens dos erros (caso exista)
        [Browsable(false)]
        public string ErrorMessages
        {
            get { return this.propertyValidatorProvider1.ErrorMessages; }
        }

        #endregion

        #region Method Members

        //Desabilita as execução dos validadores associados ao user control de manutenção
        //de itens de venda para que eles não interfiram no momento da validação da venda
        public void DisableSaleDetailValidation()
        {
            this.maintenanceControlOfSaleDetail1.PerformValidationEnabled = false;
        }

        public void RemoveSoldProductsErrorIcon()
        {
            this.errorProvider1.SetError(this.maintenanceControlOfSaleDetail1, null);
        }

        #endregion

        #region IMaintenanceControl<Sale> Members

        public Sale GetControlsValues()
        {
            this.CurrentSale.SaleDate = this.dtpSaleDate.Value;
            this.CurrentSale.SaleTotal = this.txtSaleTotal.Text.ToDecimal();
            this.CurrentSale.Customer = (Customer)this.selCustomer.SelectedValue;
            this.CurrentSale.Salesman = (Salesman)this.selSalesman.SelectedValue;
            this.CurrentSale.SaleDetailsList = this.maintenanceControlOfSaleDetail1.SoldProductsList;
            for (int i = 0; i < this.CurrentSale.SaleDetailsList.Count; i++)
            {
                this.CurrentSale.SaleDetailsList[i].Sale = this.CurrentSale;
            }

            return this.CurrentSale;
        }

        public void SetControlsValues(Sale value)
        {
            this.CurrentSale = value;

            this.dtpSaleDate.Value = this.CurrentSale.SaleDate;
            this.txtSaleTotal.Text = this.CurrentSale.SaleTotal.ToString();
            this.selCustomer.SelectedValue = this.CurrentSale.Customer;
            this.selSalesman.SelectedValue = this.CurrentSale.Salesman;
            this.maintenanceControlOfSaleDetail1.SoldProductsList = this.CurrentSale.SaleDetailsList;
        }

        public void ClearControls()
        {
            this.dtpSaleDate.Value = DateTime.Now.Date;
            this.txtSaleTotal.Text = string.Empty;
            this.selCustomer.SelectedValue = null;
            this.selSalesman.SelectedValue = null;
            this.maintenanceControlOfSaleDetail1.SoldProductsList = null;

            this.CurrentSale = new Sale();
        }

        #endregion

        #region Event Handled Members

        private void maintenanceControlOfSaleDetail1_TotalSoldUpdated(decimal obj)
        {
            this.txtSaleTotal.Text = obj.ToString();
        }

        #endregion
    }
}
